package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.GoodOrErrorQuestionAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.GoodOrErrorQuestionBean;
import bean.ItemBean;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.log.LogUtil;

/**
 *习题本 - 好题 ，错题本
 */
public class GoodOrErrorQuestionActivity extends BaseNetActivity{
    private NiceSpinner spinner_alltime,
            spinner_allsubject,
            spinner_allstate;
    private List<String> alltimeList = new ArrayList<>();
    private List<String> allsubjectList = new ArrayList<>();
    private List<String> allstateList = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private GoodOrErrorQuestionAdapter adapter;
    private ArrayList<GoodOrErrorQuestionBean.mList> lists = new ArrayList<>();
    private List<ItemBean> list = new ArrayList<>();
    private View layoutview;
    private String subject_name;
    private int page = 1;
    private int total;
    private int type_id;
    private int subject_id,type,date,question_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        setBaseRightIcon1(R.mipmap.shaixxuan, "", new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                final BottomListPopWindow popWindow  =  new BottomListPopWindow(GoodOrErrorQuestionActivity.this,list);
//                popWindow.showAtBottom(layoutview);
//                popWindow.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        toast(list.get(position).str);
//                        popWindow.dismiss();
//                    }
//                });
//            }
//        });
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        swipeRefreshLayout.setRefreshing(false);
        adapter.loadMoreEnd();
        if(baseBean.code == 200){
            GoodOrErrorQuestionBean bean = (GoodOrErrorQuestionBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,GoodOrErrorQuestionBean.class);
            total = bean.total;
            lists.addAll(bean.list) ;
            if(lists == null){
                lists = new ArrayList<>();
            }
            if(type == 1){
                for(int i =0 ;i<lists.size();i++){
                    lists.get(i).isGood = true;
                }
            }

            if(page>bean.page.total_page){
                LogUtil.app("已是最后一页");
            }else {
                page++;
                adapter.setNewData(lists);
            }
        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {
        swipeRefreshLayout.setRefreshing(false);
        adapter.loadMoreEnd();
    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_goodorerrorquestion, null);
        return R.layout.activity_goodorerrorquestion;
    }

    @Override
    protected void findViews() {
        spinner_allstate = getView(R.id.spn_allstate);
        spinner_alltime = getView(R.id.spn_alltime);
        spinner_allsubject  = getView(R.id.spn_allsubject);

        swipeRefreshLayout = getView(R.id.swiperefreshlayout);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new GoodOrErrorQuestionAdapter(R.layout.item_goodormatter,lists);
        recyclerView.setAdapter(adapter);
        //-----
        alltimeList.add("全部时间");
        alltimeList.add("本天");
        alltimeList.add("本周");
        alltimeList.add("本月");
        //0表示所有 1单选题 2多选题 3判断题 4填空题 5解答题 6实验题 7作文题
        allsubjectList.add("全部题型");
        allsubjectList.add("单选题");
        allsubjectList.add("多选题");
        allsubjectList.add("判断题");
        allsubjectList.add("填空题");
        allsubjectList.add("解答题");
        allsubjectList.add("实验题");
        allsubjectList.add("作文题");
        allstateList.add("全部状态");
        allstateList.add("已消灭");
        allstateList.add("未消灭");
        spinner_allstate.attachDataSource(allstateList);
        spinner_allsubject.attachDataSource(allsubjectList);
        spinner_alltime.attachDataSource(alltimeList);
        //时间 spinner点击
        spinner_alltime.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                date = i;
                page = 1;
                lists.clear();
                adapter.notifyDataSetChanged();
                loadData(type_id);
            }
        });
        //题型 spinner点击
        spinner_allsubject.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                page = 1;
                lists.clear();
                type_id = position;
                adapter.notifyDataSetChanged();
                loadData(type_id);

            }
        });
        //状态 spinner点击
        spinner_allstate.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                question_status = position;
                page = 1;
                lists.clear();
                adapter.notifyDataSetChanged();
                loadData(type_id);
            }
        });
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEnableLoadMore(true);
        adapter.openLoadAnimation();
        adapter.disableLoadMoreIfNotFullPage();
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                lists.clear();
                loadData(0);
            }
        });
        //加载更多
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        loadData(0);
                    }
                },500);

            }
        });
        //item点击事件
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                try {
//                    Bundle bundle = new Bundle();
//                    bundle.putInt("book_id", lists.get(position).book_id);
//                    bundle.putInt("subject_id", subject_id);
//                    bundle.putInt("position", position);
//                    bundle.putInt("type", type);
//                    bundle.putString("subject_name", subject_name);
//                    bundle.putString("date", date + "");
//                    bundle.putInt("type_id", type_id);
//                    bundle.putInt("question_status", question_status);
//                    bundle.putInt("total", total);
//                    bundle.putSerializable("list", lists);
//                    jumpTo(GoodOrErrorQuestionDetailActivity.class, 0, bundle);
//                }catch (Exception e){
//
//                }
//            }
//        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                if(view.getId() == R.id.bt_review){
                    Bundle bundle = new Bundle();
                    bundle.putInt("book_id",lists.get(position).book_id);
                    bundle.putInt("subject_id",subject_id);
                    bundle.putInt("position",position);
                    bundle.putInt("type",type);
                    bundle.putString("subject_name",subject_name);
                    bundle.putString("date",date+"");
                    bundle.putInt("type_id",type_id);
                    bundle.putInt("question_status",question_status);
                    bundle.putInt("total",total);
                    bundle.putSerializable("list", lists);
                    jumpTo(GoodOrErrorQuestionDetailActivity.class,0,bundle);
                } }catch (Exception e){

                }
            }
        });
        adapter.setRichOnclick(new GoodOrErrorQuestionAdapter.richOnclick() {
            @Override
            public void richOnclick(int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("book_id",lists.get(position).book_id);
                bundle.putInt("subject_id",subject_id);
                bundle.putInt("position",position);
                bundle.putInt("type",type);
                bundle.putString("subject_name",subject_name);
                bundle.putString("date",date+"");
                bundle.putInt("type_id",type_id);
                bundle.putInt("question_status",question_status);
                bundle.putInt("total",total);
                bundle.putSerializable("list", lists);
                jumpTo(GoodOrErrorQuestionDetailActivity.class,0,bundle);
            }
        });
    }

    @Override
    protected void formatData() {
//        for(int i=0;i<10;i++){
//            GoodOrErrorQuestionBean.mList beans = new GoodOrErrorQuestionBean.mList();
//            //beans.setState("已预习");
//            lists.add(beans);
//        }
        ItemBean bean = new ItemBean();
        bean.setStr("按截止时间排序");
        ItemBean bean1 = new ItemBean();
        bean1.setStr("按布置时间排序");
        list.add(bean);
        list.add(bean1);
        loadData(0);
    }
    private void loadData(int type_id){
        Map<String,String> map = new HashMap<>();
        map.put("subject_id",""+subject_id);
        map.put("type",""+type);//1:好题本（默认） 2:错题本
        map.put("question_status",""+question_status);//0:全部（默认） 1:已消灭 2:未消灭
        map.put("date",""+date);//date 0所有（默认） 1当天 2当周 3当月 指定日期格式：2018-08-09
        map.put("type_id",""+type_id);//0表示所有 1单选题 2多选题 3判断题 4填空题 5解答题 6实验题 7作文题
        map.put("page",""+page);
        map.put("page_number","10");
        post("Book/bookList",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
            type = bundle.getInt("type");//1:好题本（默认） 2:错题本
            subject_id = bundle.getInt("subject_id");
            subject_name = bundle.getString("subject_name");
            if(type == 1){
                setTitle(subject_name+"好题本");
                spinner_allstate.setVisibility(View.GONE);
            }else {
                setTitle(subject_name+"错题本");
            }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0){
            page = 1;
            lists.clear();
            loadData(type_id);
        }
    }
}
